// $Id: ArtistDaoImpl.java 72 2006-08-04 13:12:28Z huijzer $

/*    
	Aoide is a web-based music catalog using the Spring Framework.
	Copyright (C) 2006  Arjan Huijzer

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    
    You can contact Arjan Huijzer by sending an e-mail to huijzer@gmail.com.
*/

package org.aoide.core.persistence;

import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class ArtistDaoImpl extends HibernateDaoSupport implements ArtistDao {

	/** Apache commons logger. */
	Log log = LogFactory.getLog(ArtistDaoImpl.class);
	
	public Collection loadAll() throws DataAccessException {
		Collection artists = getHibernateTemplate().find("from Artist artist order by name");
		if (log.isDebugEnabled()) {
			log.debug("Loaded " + artists.size() + " artists.");
		}
		return artists;
	}

	public Artist load(String id) throws DataAccessException {
		Artist artist = (Artist) getHibernateTemplate().load(Artist.class, id);
		if (log.isDebugEnabled()) {
			log.debug("Loaded " + artist.toString());
		}
		return artist;
	}    
	
	public void save(Artist obj) throws DataAccessException {
		getHibernateTemplate().save(obj);
	}

	public void update(Artist obj) throws DataAccessException {
		getHibernateTemplate().update(obj);
	}
	
	public int getCount() throws DataAccessException {
		List values = getHibernateTemplate().find("select count(id) from Artist artist");
		int count = ((Integer)values.get(0)).intValue();
		return count;
	}

	public List findByName(String name) throws DataAccessException {
		List artists = getHibernateTemplate().find("from Artist artist where artist.name=?", name);
		if (log.isDebugEnabled()) {
			log.debug("Found " + artists.size() + " artist(s) with name [" + name + "]");
		}
		return artists;
	}

	public List findByNameStart(String name) throws DataAccessException {
		List artists = getHibernateTemplate().find("from Artist artist where artist.name like ? order by name", name + "%");
		if (log.isDebugEnabled()) {
			log.debug("Found " + artists.size() + " artist(s) with name that starts with [" + name + "]");
		}
		return artists;
	}
	
	public List findByNameStartWithDigit() throws DataAccessException {
		List artists = getHibernateTemplate().find("from Artist artist where artist.name < 'a' order by name");
		if (log.isDebugEnabled()) {
			log.debug("Found " + artists.size() + " artist(s) with name that starts with digits.");
		}
		return artists;
	}
	
}
